<template>
  <el-dialog :title="form.id ? '编辑' : '新增'" v-model="visible" :close-on-click-modal="false" draggable>
    <el-form ref="dataFormRef" :model="form" :rules="dataRules" formDialogRef label-width="100px">
      <el-row :gutter="24">
        <el-col :span="12">
          <el-form-item label="请假类型" prop="类型">
            <el-input v-model="form.name" placeholder="请输入请假类型" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="24" style="margin-top: 10px;">
        <el-col :span="24">
          <el-form-item label="计算周期" prop="cycle">
            <el-select v-model="form.cycle" placeholder="">
              <el-option label="月" :value="0" />
              <el-option label="年" :value="1" />
              <el-option label="无限" :value="2" />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="24" style="margin-top: 10px;">
        <el-col :span="24">
          <el-form-item label="最小单位" prop="length">
            <el-select v-model="form.length" placeholder="">
              <el-option label="0.5天" :value="0" />
              <el-option label="1.0天" :value="1" />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="24" style="margin-top: 10px;">
        <el-col :span="24">
          <el-form-item label="最大天数" prop="provideDays">
            <el-input v-model="form.provideDays" placeholder="请输入天数" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="24" style="margin-top: 10px;">
        <el-col :span="24">
          <el-form-item label="是否影响全勤" prop="bonusFlag">
            <el-select v-model="form.bonusFlag" placeholder="">
              <el-option label="是" :value="0" />
              <el-option label="否" :value="1" />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="visible = false" formDialogRef>取消</el-button>
        <el-button type="primary" @click="onSubmit" formDialogRef>确认</el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script setup lang="ts" name="QrLeaveTypeDialog">
import { useDict } from '/@/hooks/dict';
import { useMessage } from "/@/hooks/message";
import { getObj, addObj, putObj } from '../../../../api/flow/leaveType/qrLeaveType'
import { rule } from '/@/utils/validate';
const emit = defineEmits(['refresh']);

// 定义变量内容
const dataFormRef = ref();
const visible = ref(false)
// 定义字典

// 提交表单数据
const form = reactive({
  id: "",
  name: "",
  cycle: "",
  length: "",
  provideDays: "",
  bonusFlag: "",
});

// 定义校验规则
const dataRules = ref({
})

// 打开弹窗
const openDialog = (id: string) => {
  visible.value = true
  form.id = ''

  // 重置表单数据
  nextTick(() => {
    dataFormRef.value?.resetFields();
  });

  // 获取qrLeaveType信息
  if (id) {
    form.id = id
    getqrLeaveTypeData(id)
  }
};

// 提交
const onSubmit = () => {
  dataFormRef.value.validate((valid: boolean) => {
    if (!valid) {
      return false
    }

    // 更新
    if (form.id) {
      putObj(form).then(() => {
        useMessage().success('修改成功')
        visible.value = false // 关闭弹窗
        emit('refresh')
      }).catch((err: any) => {
        useMessage().error(err.msg)
      })
    } else {
      addObj(form).then(() => {
        useMessage().success('添加成功')
        visible.value = false // 关闭弹窗
        emit('refresh')
      }).catch((err: any) => {
        useMessage().error(err.msg)
      })
    }
  })
}

// 初始化表单数据
const getqrLeaveTypeData = (id: string) => {
  // 获取数据
  getObj(id).then((res: any) => {
    Object.assign(form, res.data)
  })
};

// 暴露变量
defineExpose({
  openDialog
});
</script>